package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.LotBindingAdapter;
import com.glory.dgcpcpda.adapter.LotShelvesLotInfoAdapter;
import com.glory.dgcpcpda.adapter.VehicleBindingAdapter;
import com.glory.dgcpcpda.adapter.VehicleInfoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.LotBindingVehicleEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.PackageEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LotUpVehicleBindingActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etShelves)
    FreeUI_ClearEditText etShelves;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etVehicle)
    FreeUI_ClearEditText etVehicle;
    @BindView(R.id.rvVehicleList)
    RecyclerView rvVehicleList;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    private VehicleBindingAdapter mVehicleInfoAdapter;
    private LotInfoEntity lotInfoEntity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_binding;
    }

    @Override
    protected void initView() {
        initLayout();
        initEdit(etLotID, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(LotUpVehicleBindingActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfoEntity = response;

//                    if (!lotInfoEntity.getPACKAGETYPE().contains("SOP")){
//                        lotInfoEntity = null;
//                        etLotID.setText("");
//                        etLotID.requestFocus();
//                        showToast("该批次不是SOP产品不允许在此功能操作",false);
//                        return;
//                    }

                    tvPackageType.setText(response.getLOT().getMAINMATTYPE());

                    if("COM".equals(response.getLOT().getCOMCLASS().trim())){
                        lotInfoEntity = null;
                        etLotID.setText("");
                        etLotID.requestFocus();
                        showToast("当前批次已完工，不允许上架", false);
                        return;
                    }else if("FIN".equals(response.getLOT().getCOMCLASS().trim())){
                        lotInfoEntity = null;
                        etLotID.setText("");
                        etLotID.requestFocus();
                        showToast("当前批次已终止，不允许上架", false);
                        return;
                    }else if("SCHD".equals(response.getLOT().getCOMCLASS().trim())){
                        lotInfoEntity = null;
                        etLotID.setText("");
                        etLotID.requestFocus();
                        showToast("当前批次未投产，不允许上架", false);
                        return;
                    }

                    etVehicle.requestFocus();
//                    requestTestBank(response);
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });
        });

        initEdit(etVehicle,s ->{
            if (getText(etLotID).equals("") || lotInfoEntity == null){
                showToast("请先扫描批次",false);
                return;
            }
            if (mVehicleInfoAdapter.getData().contains(s)){
                showToast("当前载具已添加",false);
                etVehicle.setText("");
                return;
            }

            LotBindingVehicleEntity lotBindingVehicleEntity = new LotBindingVehicleEntity();
            lotBindingVehicleEntity.setLotID(getText(etLotID));
            lotBindingVehicleEntity.setVehicleCode(s);
            for (LotBindingVehicleEntity datum : mVehicleInfoAdapter.getData()) {
                if (datum.getLotID().equals(getText(etLotID))){
                    etLotID.setText("");
                    etVehicle.setText("");
                    etLotID.requestFocus();
                    showToast("当前批次号已存在",false);
                    return;
                }
                if (datum.getVehicleCode().equals(s)){
                    etLotID.setText("");
                    etVehicle.setText("");
                    etLotID.requestFocus();
                    showToast("当前载具号已存在",false);
                    return;
                }
            }
            mVehicleInfoAdapter.addData(lotBindingVehicleEntity);
            etLotID.setText("");
            etVehicle.setText("");
            etLotID.requestFocus();
        });

    }

    private void requestTestBank(LotInfoEntity entity) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                (new TypeToken<BaseResponseBean<PackageEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    String storageid = response.body().getResponse().getBody().getSTORAGEID();
                    if (getText(etShelves).equals("") || getText(etShelves).equals(storageid)) {
                        etShelves.setText(storageid);
                    } else {
                        FreeApi_DialogUtils.getConfirmAndCancelDialog(LotUpVehicleBindingActivity.this, "货架号推荐不一致是否继续!", new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                etShelves.setText(storageid);
                            }
                        }, () -> {
                        }).show(getSupportFragmentManager(), "SUBMIT_DIALOG");
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TESTBANKSUGGESTLOCATOR");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("ACTIONTYPE", "");
        jsonObject.addProperty("LOTID", entity.getLOT().getLOTID());
        NetUtils.requestPostNet("CPC.TESTBANKSUGGESTLOCATOR", "TESTBANKSUGGESTLOCATOR", this, jsonObject, callBack);
    }

    private void initLayout() {
        tvTitle.setText("批次上架(载具绑定)");
        tvBaseSubmit.setText("绑定");
        setMainLayout(llMain);

        initRecyclerView(rvVehicleList, this);
        mVehicleInfoAdapter = new VehicleBindingAdapter(null);
        mVehicleInfoAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mVehicleInfoAdapter.isFirstOnly(true);
        rvVehicleList.setAdapter(mVehicleInfoAdapter);

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (lotInfoEntity == null){
                    showToast("请先扫描批次信息",false);
                    return;
                }

//                if (!lotInfoEntity.getPACKAGETYPE().contains("SOP")){
//                    showToast("该批次不是SOP产品不允许在此功能操作",false);
//                    return;
//                }
                if (mVehicleInfoAdapter.getData().size() == 0){
                    showToast("请扫描载具信息",false);
                    return;
                }
                List<LotList> lotList = new ArrayList<>();
                for (LotBindingVehicleEntity entity:mVehicleInfoAdapter.getData()){
                    lotList.add(new LotList(entity.getLotID()));
                }




                for (LotList lotInfo:lotList){
                    ArrayList<String> strings = new ArrayList<>();
                    for (LotBindingVehicleEntity entity:mVehicleInfoAdapter.getData()){
                        if (lotInfo.getLOTID().equals(entity.getLotID())){
                            strings.add(entity.getVehicleCode());
                            lotInfo.setATTRIBUTE1(strings);
                        }
                    }
                }



                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("载具绑定成功", true);
                                    lotList.clear();
                                    lotInfoEntity = null;
                                    mVehicleInfoAdapter.setNewData(null);
                                    tvPackageType.setText("");
                                    etLotID.setText("");
                                    etShelves.setText("");
                                    etVehicle.setText("");
                                    etLotID.requestFocus();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(), "CPCMOVELOCATOR");
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", "BIND");
                        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
                        map.addProperty("LOCATORID", getText(etShelves).toUpperCase());
                        NetUtils.requestPostNet("CPC.CPCMOVELOCATOR", "CPCMOVELOCATOR", this, map, callBack);
                    }
                });
                break;
        }
    }
}